package bawei.com.bilibili;

import android.app.Activity;



import android.content.Intent;

import android.os.Build;
import android.os.Bundle;



import android.os.Handler;

import android.support.annotation.RequiresApi;

import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;



import android.support.v7.app.ActionBar;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;


import android.support.v7.widget.Toolbar;


import com.squareup.okhttp.Request;


import com.squareup.okhttp.Response;


import java.util.List;


import bawei.com.adapter.TopicAdapter;
import bawei.com.model.BaseCallback;

import bawei.com.model.Network;

import bawei.com.model.OkHttpHelper;


import bawei.com.model.bean.TopicBean;

/**
 * 类描述:
 * 作者：cwm
 * 时间:2017/3/19 18:22
 * 邮箱:18310832074@163.com
 */

public class TopicActivity extends FragmentActivity {

    private Toolbar layout_toolbar;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recycle;


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic_center);
        layout_toolbar = (Toolbar) findViewById(R.id.toolbar);
        layout_toolbar.setTitle("话题中心");
        layout_toolbar.setLogo(R.drawable.action_button_back_pressed_light);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
        recycle = (RecyclerView) findViewById(R.id.recycle);
        recycle.setLayoutManager(new LinearLayoutManager(this));
        getRefresh();
        getdate();
    }

    public void getdate(){

        OkHttpHelper.getinstance().get(Network.HUATI, new BaseCallback<TopicBean>() {

            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, TopicBean topicBean) {

                final List<TopicBean.ListBean> list = topicBean.getList();
                TopicAdapter topicAdapter=new TopicAdapter(TopicActivity.this,list);
                recycle.setAdapter(topicAdapter);
                topicAdapter.setOnclientLiker(new TopicAdapter.OnclientLiker() {

                    @Override
                    public void OnclientLikerRecy(int p) {
                        Intent intent=new Intent(TopicActivity.this,WebViewActivity.class);
                        intent.putExtra("title",list.get(p).getTitle());
                        intent.putExtra("web",list.get(p).getLink());
                        startActivity(intent);
                    }
                });
            }
            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });


    }
    public void  getRefresh(){

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 停止刷新
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 1000); // 2秒后发送消息，停止刷新
            }
        });
        // 设置下拉圆圈上的颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.pink_text_color);
        // 设置手指在屏幕下拉多少距离会触发下拉刷新
        swipeRefreshLayout.setDistanceToTriggerSync(400);
        swipeRefreshLayout.setProgressBackgroundColor(android.R.color.white);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
    }

}
